package com.jjwww.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjwww.common.QueryPageParam;
import com.jjwww.common.Result;
import com.jjwww.entity.Orders;
import com.jjwww.service.impl.OrdersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjwww
 * @since 2025-10-23
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    public OrdersServiceImpl orderService;

    //按钮添加save
    @PostMapping("/save")
    public Result save(@RequestBody Orders orders){
        return orderService.save(orders)?Result.suc():Result.fail();
    }
    //按钮修改update
    @PostMapping("/update")
    public Result update(@RequestBody Orders orders){
        return orderService.updateById(orders)?Result.suc():Result.fail();
    }
    //按钮删除delete
    @GetMapping("/del")
    public Result del(String id){
        return orderService.removeById(id)?Result.suc():Result.fail();
    }
    //按钮查询
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name= (String)param.get("name");
        //总记录数，当前页
        Page<Orders> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Orders::getName,name);
        }
        IPage result = orderService.pageBB(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }
    //查询 加载参数
    @GetMapping("/list")
    public Result list(){
        List list = orderService.list();
        return Result.suc(list);
    }





    //页面查询
    @GetMapping("/list1")
    public List<Orders> list1(){
        return orderService.list();
    }
    //新增
    @PostMapping("/save1")
    public boolean save1(@RequestBody Orders orders){
        return orderService.save(orders);
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Orders orders){
        return orderService.updateById(orders);
    }
    //新增或修改
    @PostMapping("/saveOrmod")
    public boolean saveOrmod(@RequestBody Orders orders){
        return orderService.saveOrUpdate(orders);
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return orderService.removeById(id);
    }
    //模糊查询
    @PostMapping("listP")
    public List<Orders> listp(@RequestBody Orders orders){
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Orders::getName, orders.getName());
        return orderService.list(lambdaQueryWrapper);

    }





}
